#include <bits/stdc++.h>


int trap(std::vector<int>& height) {
    int n = height.size();
    if (n == 0) {
        return 0;
    }

    // 左前缀最大值
    std::vector<int> leftMax(n);
    leftMax[0] = height[0];
    for (int i = 1; i < n; ++i) {
        leftMax[i] = std::max(leftMax[i - 1], height[i]);
    }

    // 右前缀组滴啊之
    std::vector<int> rightMax(n);
    rightMax[n - 1] = height[n - 1];
    for (int i = n - 2; i >= 0; --i) {
        rightMax[i] = std::max(rightMax[i + 1], height[i]);
    }

    int ans = 0;
    for (int i = 0; i < n; ++i) {
        ans += std::min(leftMax[i], rightMax[i]) - height[i];
    }
    return ans;
}

int main() {
    std::vector<int> heights = {4, 2, 0, 3, 2, 5};
    int capacity = trap(heights);

    printf("给定数组的最大盛水量: %d\n", capacity);
}